﻿Public Class FormDonViTinh

    Private Sub FormDonViTinh_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        LoadDVT()
    End Sub

    Private Sub LoadDVT()
        Dim dt As DataTable
        dt = DonViTinhDAO.Select_All()
        dgvDonViTinh.DataSource = dt
    End Sub

    Private Sub btnThem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnThem.Click
        Dim DVTDTO As New DonViTinhDTO
        DVTDTO.TenDVT = txtDonViTinh.Text
        If (DonViTinhDAO.Insert(DVTDTO) = 1) Then
            MsgBox("Thêm thành công", MsgBoxStyle.OkOnly, "")
        Else
            MsgBox("Không thê thêm Đon Vị Tính này", MsgBoxStyle.OkOnly, "")
        End If
        LoadDVT()
        Clear()
    End Sub

    Private Sub btnThoat_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnThoat.Click
        Me.Close()
    End Sub

    Private Sub Clear()
        txtDonViTinh.Clear()
    End Sub

    Private Sub dgvDonViTinh_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles dgvDonViTinh.Click
        If dgvDonViTinh.SelectedRows.Count > 0 Then
            txtDonViTinh.Text = dgvDonViTinh.SelectedRows(0).Cells(1).Value.ToString()
        End If
    End Sub

    Private Sub btnXoa_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnXoa.Click
        Dim maDVT As Integer
        If dgvDonViTinh.SelectedRows.Count > 0 Then
            maDVT = Convert.ToInt32(dgvDonViTinh.SelectedRows(0).Cells(0).Value.ToString())
            If (DonViTinhDAO.Delete(maDVT) = 1) Then
                MsgBox("Xoa thanh công", MsgBoxStyle.OkOnly, "")
            Else
                MsgBox("Xóa thất bại", MsgBoxStyle.OkOnly, "")
            End If
        End If
        LoadDVT()
        Clear()
    End Sub
End Class